package appapi.controller.oider;

import appapi.Request.DeleteAuthReq;
import appapi.Request.RolepermissionReq;
import appapi.Vo.RoleAuthVo;
import appapi.common.ResponseBean;
import appapi.entity.auth.ModulelistEntity;
import appapi.entity.auth.RolepermissionEntity;
import appapi.service.RolepermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色权限
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:27
 */
@Api(tags = "角色权限")
@RestController
@RequestMapping("/api/roleAuth")
public class RolepermissionController {
    private static final Logger logger = LoggerFactory.getLogger(RolepermissionController.class);
    @Autowired
    private RolepermissionService rolepermissionService;

    /**
     * 列表
     */
    @GetMapping("/getRoleModuleList")
    @ApiOperation(value = "获取角色功能权限列表")
    public ResponseBean<ModulelistEntity> getList(@Valid RolepermissionReq rolepermissionReq) {
        try {
            List<ModulelistEntity> pageInfo = rolepermissionService.list(rolepermissionReq);
            return ResponseBean.success(pageInfo);
        } catch (Exception e) {
            logger.error("获取角色功能权限列表异常", e);
            return ResponseBean.fail("获取角色功能权限列表异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping(value = "/addRoleAuth")
    @ApiOperation(value = "新增角色权限")
    public ResponseBean insert(@RequestBody RoleAuthVo roleAuthVo) {
        try {
            if(roleAuthVo.getRoleModuIds().size()==0)
            {
                return ResponseBean.fail("请选择角色权限");
            }
            //获取角色现有权限
            Long roleId =roleAuthVo.getRoleId();
            RolepermissionReq req =new RolepermissionReq();
            req.setRoleId(roleId);
            List<RolepermissionEntity> oldList =rolepermissionService.detail(roleId);
            List<RolepermissionEntity> addList = new ArrayList<>();

            //判断新增加的角色权限
            for (Long authid:roleAuthVo.getRoleModuIds()
                 ) {
                //新增
                if(oldList.stream().filter(RolepermissionEntity -> RolepermissionEntity.getModuId().equals(authid)).count()==0) {
                    RolepermissionEntity entity = new RolepermissionEntity();
                    entity.setRoleId(roleAuthVo.getRoleId());
                    entity.setModuId(authid);
                    entity.setCreateTime(LocalDateTime.now());
                    entity.setUpdateTime(LocalDateTime.now());

                    addList.add(entity);
                }
            }
            //删除原有权限
            List<Long> delList =new ArrayList<>();
            for (RolepermissionEntity oldAuth:oldList
            ) {
                if(roleAuthVo.getRoleModuIds().stream().filter(moduId -> moduId.equals(oldAuth.getModuId())).count()==0) {
                    delList.add(oldAuth.getModuId());
                }
            }
            if(delList.size()>0) {
                rolepermissionService.deleteBatch(roleId,delList);
            }
            if(addList.size()>0) {
                rolepermissionService.insertBatch(addList);
            }
            return ResponseBean.success("授权成功");
        } catch (Exception e) {
            logger.error("角色授权出错", e);
            return ResponseBean.fail("角色授权出错");
        }
    }


    /**
     * 批量删除
     */
    @PostMapping(value = "/deleteRoleAuth")
    @ApiOperation(value = "删除角色权限")
    public ResponseBean delete(@RequestBody DeleteAuthReq deleteAuthReq) {
        try {

            int iRet=rolepermissionService.deleteBatch(deleteAuthReq.getRoleId(),deleteAuthReq.getModuIds());
            if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除角色权限出错", e);
            return ResponseBean.fail("删除角色权限出错");
        }
    }
}
